/*
 * sdmmc_spi.h
 *
 *  Created on: Oct 16, 2013
 *      Author: Ken Arok
 */

#ifndef SDMMC_SPI_H_
#define SDMMC_SPI_H_

#include "sdmmc_protocol.h"

/** \brief Activate SD/MMC device.
 *
 * \return true if success. Unless false.
 */
extern bool sdmmc_spi_select_device(void);

/** \brief De-activate SD/MMC device.
 *
 * \return true if success. Unless false.
 */
extern bool sdmmc_spi_deselect_device(void);

/** \brief Initialize SD/MMC.
 *
 */
extern void sdmmc_spi_init(void);

/** \brief Setup SD/MMC device.
 *
 */
extern void sdmmc_spi_setup_device(uint32_t _clock);

/** \brief Send Dummy Clock (more than 74 cycle) to SD/MMC.
 *
 */
extern void sdmmc_spi_send_clock(void);

/** \brief Send Command to SD/MMC.
 *
 * \param _cmd	SD/MMC Command.
 * \param _arg	Argument of command.
 *
 * \return Response R1.
 */
extern uint8_t sdmmc_spi_send_cmd(uint8_t _cmd, uint32_t _arg);

/** \brief Get Command Response from SD/MMC.
 *
 */
extern uint32_t sdmmc_spi_get_response(void);

/** \brief Get CID or CSD register value.
 *
 * \param _pbuf		Pointer to store register value.
 *
 * \return 0 if success. Unless error.
 */
extern uint8_t sdmmc_spi_get_cid_csd_register(uint8_t *_pbuf);

/** \brief Get SCR register value.
 *
 * \param _pbuf		Pointer to store register value.
 *
 * \return 0 if success. Unless error.
 */
extern uint8_t sdmmc_spi_get_scr_register(uint8_t *_pbuf);

/** \brief Read 32-bit data value from SD/MMC.
 *
 * \param _value	Pointer to store data value.
 *
 * \return 0 if success. Unless error.
 */
extern uint8_t sdmmc_spi_read_dword(uint32_t *_value);

/** \brief Write 32-bit data value to SD/MMC.
 *
 * \param _value	Pointer to data to be written.
 *
 * \return 0 if success. Unless error.
 */
extern uint8_t sdmmc_spi_write_dword(uint32_t _value);

/** \brief Read block of data from SD/MMC.
 *
 * \param _dest		Pointer to destination where data will be stored.
 * \param _nb_block	Number of block to be read.
 *
 * \return 0 if success. Unless error.
 */
extern uint8_t sdmmc_spi_read_block(void *_dest, uint16_t _nb_block);

/** \brief Write block of data to SD/MMC.
 *
 * \param _src		Pointer to source of data to be written.
 * \param _nb_block	Number of block to be read.
 *
 * \return 0 if success. Unless error.
 */
extern uint8_t sdmmc_spi_write_block(const void *_src, uint16_t _nb_block);

/** \brief Wait End of Writing Block.
 *
 * \return 0 if success. Unless error.
 */
extern uint8_t sdmmc_spi_wait_end_of_write_block(void);

#endif /* SDMMC_SPI_H_ */
